<?php
/*
 * 手机客户端店铺-下订单接口
 * 2014-03-13
 * 2014-05-08 (已查)
 */
include "inc/public_header.php";

include "func/user.php";				//用户信息处理函数


$reJson = array(); //构建返回值

$goodsId = $_REQUEST["goodsId"];
$key = $_REQUEST["key"];
$json = $_REQUEST["json"];
//$json 		= '{"address": [{"name": "\u91d1\u4e1c\u56fd","province": "\u4e0a\u6d77\u5e02","city": "\u4e0a\u6d77","county": "\u666e\u9640\u533a","detailAddress": "\u5149\u590d\u897f\u8def","postCode": "","mobile": "13564838623","phone": "-"}],"list":[{"goods":[{"goodsNum":"4","goodsId":"60"},{"goodsNum":"3","goodsId":"69"}],"shopId":"66"},{"goods":[{"goodsNum":"2","goodsId":"70"},{"goodsNum":"1","goodsId":"72"}],"shopId":"67"}]}';		

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
if(!$uInfoArr){
	echo json_encode(array("success"=>"0","errCode"=>"0","msg"=>"没有获取到用户信息"));
	die;
}
$uid = $uInfoArr["uid"];//用户信息主键


//$uid = "10638";



//商品介绍信息
$sql = "select receiveName,province,city,district,street,zipcode,mobile,phone from `user_addresses` where  uid='".$uid."' ORDER BY uaid desc limit 1"; 
$db->query($sql);
if($db->next_record()){
	$receiveName	= $db->f("receiveName");
	$province 		= $db->f("province");
	$city	  		= $db->f("city");
	$district	   	= $db->f("district");
	$street	   		= $db->f("street");
	$zipcode	   	= $db->f("zipcode");
	$mobile	   		= $db->f("mobile");
	$phone	   		= $db->f("phone");
}
$ship_address=$province.$city.$district.$street;
if ($receiveName){
$address = array();
$address = array(
		"name"=>$receiveName
		,"province"=>$province
		,"city"=>$city
		,"county"=>$district
		,"detailAddress"=>$street
		,"postCode"=>$zipcode
		,"mobile"=>$mobile
		,"phone"=>$phone
	);

}
if ($goodsId){
$sql = "SELECT shop_id,aprice from shop_product where product_id='".$goodsId."'"; 
$db->query($sql);
if($db->next_record()){
	$shop_id	= $db->f("shop_id");
	$aprice 		= $db->f("aprice");
}
$sql="select c.shop_id from shop_order_detail a inner join shop_order b on a.pid=b.id INNER JOIN shop_product c on a.product_id=c.product_id where b.uid='".$uid."' and b.del_type='0' and c.shop_id in ('81','86')";
$dc=$db1->query($sql);
if($db1->next_record()){
$shop_id1 = $db1->f('shop_id');
}
$sql="select c.product_id from shop_order_detail a inner join shop_order b on a.pid=b.id INNER JOIN shop_product c on a.product_id=c.product_id where b.uid='".$uid."' and b.del_type='0' and c.product_id='".$goodsId."' and c.shop_id in ('87','88','92','93')";
$dc=$db1->query($sql);
if($db1->next_record()){
$product_id1 = $db1->f('product_id');
}
if (($shop_id=="81" || $shop_id=="86") && $shop_id1){
	$errtype="1";
}
if (($shop_id=="87" || $shop_id=="88" || $shop_id=="92" || $shop_id=="93") && $product_id1){
	$errtype="2";
}
if ($errtype == 1){
	echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"亲，每人只能限购一套，您已订购了大全套或小全套，可以继续购买大学生表和孙信德表！"));
	die;
}elseif ($errtype == 2){
if ($shop_id == "92"){
	echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"亲，每人只能限购一本，您已订购了该款“墨屏”，可以继续购买其他“墨屏”！"));
	die;
}elseif ($shop_id == "93"){
	echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"亲，每人只能限购一个，您已订购了该款“把玩件”，可以继续购买其他“把玩件”！"));
	die;	
}else{
	echo json_encode(array("success"=>"0","errCode"=>"-1","msg"=>"亲，每人只能限购一支，您已订购了该款手表，可以继续购买其他手表！"));
	die;
}
}else{
$sql="insert into `shop_order` (`uid`,`shop_id`,`tprice`,`tcount`,`ctime`,`ship_address`,`ship_province`,`ship_man`,`ship_pcode`,`ship_mobile`,`yunfei`,`ship_type`) values 
	('".$uid."','".$shop_id."','".$aprice."','1','".date("Y-m-d H:i:s")."','".$ship_address."','".$province."','".$receiveName."','".$zipcode."','".$mobile."','0','1') ";
    $db1->query($sql);
    $ids=$db1->insert_id();
	$tids = $ids.";";
$db2->query("insert into `shop_order_detail` (`pid`,`order_status_id`,`product_id`,`tcount`,`tprice`,`ctime`,`sptype`) values('".$ids."','1','".$goodsId."','1','".$aprice."','".date("Y-m-d H:i:s")."','".$key."')");
$db4->query("delete from `shop_product_gwc` where  product_id='".$goodsId."' and uid='".$uid."'");
$db3->query("insert into `shop_order_history` (`order_id`,`order_status_id`,`date_added`) values('".$ids."','1','".date("Y-m-d H:i:s")."')");
$orderTime = date('Y-m-d H:i:s', time());
$rno = str_replace(array('-',':',' '), '', $orderTime);
$sql="insert into chinapay_running_number (rno,order_id) values ('".$rno."','".$tids."')";
$db->query($sql);
$reJson["success"] = "1";
$reJson["orderNum"] = $rno;
$reJson["orderTime"] = $orderTime;
$reJson["address"] = $address;
$payType[] = array(
		"key"=>"1"			
		,"value"=>"银联移动支付"		
	);
$payType[] = array(
		"key"=>"2"			
		,"value"=>"线下银行支付"		
	);

$reJson["package"] = "1";
$reJson["payType"] = $payType;
$reJson["pay"] = "$aprice";
$reJson["remark"] = "预付款";
echo json_encode($reJson);
}
}else{
$arr=json_decode($json,true);
$tids="";

foreach($arr['list'] as $val){
	$arr3=$val['goods'];
	foreach($arr3 as $v){
		$sql = "select b.product_name,b.quantity,b.salesvolume,a.product_id,b.shangjia from (SELECT product_id from shop_product_gwc where uid='".$uid."') a right join  shop_product  b on a.product_id=b.product_id where b.product_id = '".$v['goodsId']."'";
		$db1->query($sql);
		$db1->next_record();
		if (!$db1->f("product_id")){
			$list[]=$db1->f("product_name");
		}
		if (($db1->f("quantity"))-($db1->f("salesvolume"))<$v['goodsNum']){
			$list1[]=$db1->f("product_name");
		}
		if ($db1->f("shangjia")=="0"){
			$list2[]=$db1->f("product_name");
		}
	}
}
if ($list){
	echo json_encode(array("success"=>"0","errCode"=>"1","msg"=>"该商品已经不在购物车","list"=>$list));
	die;
}else if ($list2){
	echo json_encode(array("success"=>"0","errCode"=>"3","msg"=>"商品已下架","list"=>$list2));
	die;
}else if ($list1){
	echo json_encode(array("success"=>"0","errCode"=>"2","msg"=>"商品库存不足","list"=>$list1));
	die;
}else{
$reJson["success"] = "1";
foreach($arr['list'] as $val){
	$arr3=$val['goods'];
	foreach($arr3 as $v){
		
		$z="SELECT price*'".$v['goodsNum']."' as tpr from shop_product_gwc where uid='".$uid."' and product_id='".$v['goodsId']."'";
		$db2->query($z);
		 if($db2->next_record()){
			$tpr = $db2->f('tpr');
		}
		 $arr7[]=$tpr;
		 $arr8[]=$v['goodsNum'];
		 
		 $y="SELECT b.ems_a*a.weight*'".$v['goodsNum']."' as tems,b.kd_a*a.weight*'".$v['goodsNum']."' as tkd,b.py_a*a.weight*'".$v['goodsNum']."' as tpy,b.ems_f,b.kd_f,b.py_f,b.ems_a,b.kd_a,b.py_a from shop_product a
							INNER JOIN
							shop_region_area b on a.shop_id=b.shop_id
							inner join 
							region c on b.area_id=c.area_id
							where a.product_id='".$v['goodsId']."' and a.shipping='0' and c.codes like '%0000' and c.region='".$province."'";
		$db2->query($y);
		 if($db2->next_record()){
			$tems = $db2->f('tems');
			$tkd = $db2->f('tkd');
			$tpy = $db2->f('tpy');
			$ems_f = $db2->f('ems_f');
			$kd_f = $db2->f('kd_f');
			$py_f = $db2->f('py_f');
			$ems_a = $db2->f('ems_a');
			$kd_a = $db2->f('kd_a');
			$py_a = $db2->f('py_a');
		 }
		 $arr4[]=$tems;
		 $arr5[]=$tkd;
		 $arr6[]=$tpy;
		 $tems="";
		 $tkd="";
		 $tpy="";
		 $tpr="";
	}
	if ($val['type']=="EMS"){
		$ttyf=array_sum($arr4)+$ems_f-$ems_a;
	}elseif ($val['type']=="快递"){
		$ttyf=array_sum($arr5)+$kd_f-$kd_a;
	}elseif ($val['type']=="平邮"){
		$ttyf=array_sum($arr6)+$py_f-$py_a;
	}else{
		$ttyf="0";
	}
	$ttpr=array_sum($arr7);
	$ttsum=array_sum($arr8);
	$pay[]=$ttyf+$ttpr;
	$package[]=1;
	$arr4="";
	$arr5="";
	$arr6="";
	$arr7="";
	$arr8="";
	$sql="insert into `shop_order` (`uid`,`shop_id`,`tprice`,`tcount`,`ctime`,`ship_address`,`ship_province`,`ship_man`,`ship_pcode`,`ship_mobile`,`yunfei`,`ship_type`) values 
		('".$uid."','".$val['shopId']."','".$ttpr."','".$ttsum."','".date("Y-m-d H:i:s")."','".$ship_address."','".$province."','".$receiveName."','".$zipcode."','".$mobile."','".$ttyf."','1') ";
       
		  $db1->query($sql);
          $ids=$db1->insert_id();
	$tids = $tids.$ids.";";
	
	foreach($arr3 as $v){
		
		$z="select t5.price,t5.sptype from shop_product_gwc t5
							inner join `shop_product` t1 on t5.product_id=t1.product_id
					    where  t5.product_id='".$v['goodsId']."' and t5.uid='".$uid."'";
		$db2->query($z);
		 if($db2->next_record()){
			$price = $db2->f('price');
			$sptype = $db2->f('sptype');
		}
		$db2->query("insert into `shop_order_detail` (`pid`,`order_status_id`,`product_id`,`tcount`,`tprice`,`ctime`,`sptype`) values('".$ids."','1','".$v['goodsId']."','".$v['goodsNum']."','".$v['goodsNum']*$price."','".date("Y-m-d H:i:s")."','".$sptype."')");
		$db4->query("delete from `shop_product_gwc` where  product_id='".$v['goodsId']."' and uid='".$uid."'");
		 
	}
	$db3->query("insert into `shop_order_history` (`order_id`,`order_status_id`,`date_added`) values('".$ids."','1','".date("Y-m-d H:i:s")."')");
	
	
	
		 
	
}

$orderTime = date('Y-m-d H:i:s', time());
$rno = str_replace(array('-',':',' '), '', $orderTime);
$sql="insert into chinapay_running_number (rno,order_id) values ('".$rno."','".$tids."')";
$db->query($sql);
$reJson["orderNum"] = $rno;
$reJson["orderTime"] = $orderTime;
$reJson["address"] = $address;
$pay=array_sum($pay);
$package=array_sum($package);
$payType[] = array(
		"key"=>"1"			
		,"value"=>"银联移动支付"		
	);
$payType[] = array(
		"key"=>"2"			
		,"value"=>"线下银行支付"		
	);

$reJson["package"] = "$package";
$reJson["payType"] = $payType;
$reJson["pay"] = "$pay";
echo json_encode($reJson);
}
}
?>
